/*
Copyright 2016 Tomer Goldstein
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.tomergoldst.tooltipshm;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;

class DefaultToolTipAnimator implements ToolTipAnimator {
    private AnimatorProperty animatorValue;
    @Override
    public AnimatorProperty popup(Text view, DependentLayout image, long duration) {
        view.setVisibility(Component.VISIBLE);
        AnimatorProperty abovePopup = new AnimatorProperty();
        abovePopup.alphaFrom(0).alpha(90).setCurveType(new Animator.CurveType().ANTICIPATE).setDuration(300);
        abovePopup.setTarget(image);
        abovePopup.start();
        return abovePopup;
    }

    @Override
    public AnimatorProperty popout(final Component view, final long duration, final AnimatorProperty animaAdapter) {
        animatorValue = new AnimatorProperty();
        animatorValue.alpha(0).scaleX(0).scaleY(0).setDuration(duration).setCurveType(3);
        view.setVisibility(Component.INVISIBLE);
        if (animaAdapter != null) {
            animaAdapter.end();
        }
        animator(view,animatorValue,animaAdapter);
        return animatorValue;
    }

    private void animator(Component view,AnimatorProperty animatorValue,AnimatorProperty animaAdapter){
        animatorValue.setStateChangedListener(new AnimatorProperty.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                view.setVisibility(Component.INVISIBLE);
                if (animaAdapter != null) {
                    animaAdapter.end();
                }
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
    }
}
